import config from './config.js';
import dayjs from "dayjs";

String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
}

//空判断
function doIsNull(data) {
    data = "" + data;
    return (data == "" || data == undefined || data == null || data == "null" || data == "undefined") ? true : false;
}

//数组合并，防止null
function doArrayConcat(list1, list2) {
    if (doIsNull(list2)) {
        return list1;
    }
    else {
        return list1.concat(list2);
    }
}

//数组-删除重复数据
function doArrayDeleteSame(list, key) {
    var reList = [];//返回数组
    var isList = [];//判断数据
    for(var i = 0; i < list.length; i++){//循环数组
        var isItem = list[i][key];
        if(!(isList.indexOf(isItem)>-1)){
            reList.push(list[i]);
            isList.push(isItem);
        }
    }
    return reList;
}

//分组
function doArrayGroup(arr, key) {
    var map = {}, dest = [];
    for (var i = 0; i < arr.length; i++) {
        var ai = arr[i];
        if (!map[ai[key]]) {//不存在该数组
            dest.push({
                key: ai[key],
                data: [ai]
            });
            map[ai[key]] = ai;
        } else {//存在该数组
            for (var j = 0; j < dest.length; j++) {
                var dj = dest[j];
                if (dj.key == ai[key]) {
                    dj.data.push(ai);
                    break;
                }
            }
        }
    }
    return dest;
}

//排序
function doArraySort(array, key) {
    return array.sort(function (a, b) {
        var x = a[key];
        var y = b[key];
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    })
}

//时间：当前时间转化成几分钟前几天前几月前几年前
function doGetUpdateTime(updateTime) {
    if (doIsNull(updateTime)) {
        return ''
    }
    updateTime = new Date(updateTime).getTime();
    let now = new Date().getTime()
    let second = Math.floor((now - updateTime) / (1000))
    let minute = Math.floor(second / 60)
    let hour = Math.floor(minute / 60)
    let day = Math.floor(hour / 24)
    let month = Math.floor(day / 31)
    let year = Math.floor(month / 12)
    if (year > 0) {
        return year + '年前'
    } else if (month > 0) {
        return month + '月前'
    } else if (day > 0) {
        let ret = day + '天前'
        if (day >= 7 && day < 14) {
            ret = '1周前'
        } else if (day >= 14 && day < 21) {
            ret = '2周前'
        } else if (day >= 21 && day < 28) {
            ret = '3周前'
        } else if (day >= 28 && day < 31) {
            ret = '4周前'
        }
        return ret
    } else if (hour > 0) {
        return hour + '小时前'
    } else if (minute > 0) {
        return minute + '分钟前'
    } else if (second > 0) {
        return second + '秒前'
    } else {
        return '刚刚'
    }
}

//获取url参数
function doUrlGetParam(name, url) {
    if (doIsNull(url)) {
        url = window.location.search;
    }
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = url.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
}

function doUrlAddParam(url, param) {
    if (doIsNull(param)) {
        return url;
    }
    url += url.indexOf('?') > -1 ? '&' : '?';
    url += param;
    return url;
}

//时间格式化
function doDate(d) {
    if(doIsNull(d)) return ''
    return dayjs(d).format("YYYY-MM-DD")
}

//内容高度-除去头部
function doUIHeightRemoveHeader() {
    return window.screen.height - 46;
}





export default {

    //方法
    doIsNull,
    doArrayConcat,
    doArrayDeleteSame,
    doArrayGroup,
    doArraySort,
    doGetUpdateTime,
    doUrlGetParam,
    doUrlAddParam,
    doDate,
    doUIHeightRemoveHeader,
}


